{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Theano 随机数流变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using gpu device 1: Tesla C2075 (CNMeM is disabled)\n"
     ]
    }
   ],
   "source": [
    "import theano\n",
    "import theano.tensor as T\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Theano` 的随机数变量由 `theano.sandbox.rng_mrg` 中的 `MRG_RandomStreams` 实现（`sandbox` 表示是实验代码）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from theano.sandbox.rng_mrg import MRG_RandomStreams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "新建一个 `MRG_RandomStreams(seed=12345, use_cuda=None)`  实例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "srng = MRG_RandomStreams()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "它支持以下方法：\n",
    "\n",
    "- `normal(size, avg=0.0, std=1.0, ndim=None, dtype=None, nstreams=None)` \n",
    "    - 产生指定形状的、服从正态分布 $N(avg, std)$ 的随机数变量，默认为标准正态分布 \n",
    "- `uniform(size, low=0.0, high=1.0, ndim=None, dtype=None, nstreams=None)`\n",
    "    - 产生指定形状的、服从均匀分布 $U(low, high)$ 的随机数变量，默认为 0-1 之间的均匀分布\n",
    "- `binomial(size=None, n=1, p=0.5, ndim=None, dtype='int64', nstreams=None)`\n",
    "    - 产生指定形状的、服从二项分布 $B(n,p)$ 的随机数变量\n",
    "- `multinomial(size=None, n=1, pvals=None, ndim=None, dtype='int64', nstreams=None)`\n",
    "    - 产生指定形状的、服从多项分布的随机数变量\n",
    "\n",
    "与 np.random.random 不同，它产生的是随机数变量，而不是随机数数组，因此可以将 `size` 作为参数传给它："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.10108768 -1.64354193  0.71042836 -0.77760422  0.06291872]\n",
      "[ 0.23193923  0.71880513  0.03122572  0.97318739  0.99260223]\n",
      "[0 1 0 1 1]\n"
     ]
    }
   ],
   "source": [
    "rand_size = T.vector(dtype=\"int64\")\n",
    "\n",
    "rand_normal = srng.normal(rand_size.shape)\n",
    "rand_uniform = srng.uniform(rand_size.shape)\n",
    "rand_binomial = srng.binomial(rand_size.shape)\n",
    "\n",
    "f_rand = theano.function(inputs = [rand_size], \n",
    "                         outputs = [rand_normal, rand_uniform, rand_binomial])\n",
    "\n",
    "print f_rand(range(5))[0]\n",
    "print f_rand(range(5))[1]\n",
    "print f_rand(range(5))[2]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
